/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.validasi;

import java.util.List;
import pkl51.component.TextField;
import pkl51.controller.CardKues;
import pkl51.error.EntityException;
import pkl51.model.Blok123Model;
import pkl51.view.kuesioner.PanelBlok123;
import pkl51.view.kuesioner.PanelBlok4;

/**
 *
 * @author Hindarwan
 */
public class ValidasiBlok123 extends AbstractValidasi {

    private PanelBlok123 panelBlok123;

    public void setPanelBlok123(PanelBlok123 panelBlok123) {
        this.panelBlok123 = panelBlok123;
    }

    public ValidasiBlok123() {
    }

    public void registerValidator() {
        panelBlok123.getTextFieldB1R2().setValidator(new ValidasiB1R2());
        panelBlok123.getTextFieldB1R3().setValidator(new ValidasiB1R3());
        panelBlok123.getTextFieldB1R4().setValidator(new ValidasiB1R4());
        panelBlok123.getTextFieldB1R5().setValidator(new ValidasiB1R5());
        panelBlok123.getTextFieldB1R6().setValidator(new ValidasiB1R6());
        panelBlok123.getTextFieldNKS().setValidator(new ValidasiNKS());
        panelBlok123.getTextFieldB1R8().setValidator(new ValidasiB1R8());

        panelBlok123.getTextFieldB2R1().setValidator(new ValidasiB2R1());
        panelBlok123.getTextFieldB2R2().setValidator(new ValidasiB2R2());
        panelBlok123.getTextFieldB2R3().setValidator(new ValidasiB2R3());

        panelBlok123.getTextFieldB3R1().setValidator(new ValidasiB3R1());
        panelBlok123.getTextFieldB3R4().setValidator(new ValidasiB3R4());
        panelBlok123.getTextFieldB3R2a().setValidator(new ValidasiB3R2a());
        panelBlok123.getTextFieldB3R5a().setValidator(new ValidasiB3R5a());
        panelBlok123.getTextFieldB3R2b().setValidator(new ValidasiB3R2b());
        panelBlok123.getTextFieldB3R5b().setValidator(new ValidasiB3R5b());
    }

    private class ValidasiB1R2 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().setB1R2(String.format("%03d", field.getInt()));
                return true;
            } catch (EntityException ex) {
                errorMessage("Kode kecamatan tidak ditemukan");
                return false;
            }
        }
    }

    private class ValidasiB1R3 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().setB1R3(String.format("%03d", field.getInt()));
                return true;
            } catch (EntityException ex) {
                errorMessage("Kode Desa tidak ditemukan");
                return false;
            }
        }
    }

    //Perlu diedit (blm otomatis 0)
    private class ValidasiB1R4 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().getValidasiB1R4(field.getText());
                return true;
            } catch (EntityException ex) {
                errorMessage("Kode nomor blok sensus tidak ditemukan");
                return false;
            }
        }
    }

    private class ValidasiB1R5 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().getValidasiB1R5(String.format("%02d", field.getInt()));
                Blok123Model.getInstance().setB1R5(String.format("%02d", field.getInt()));
                return true;
            } catch (EntityException ex) {
                errorMessage("Kode Nomor Urut Sampel Blok Sensus tidak ditemukan");
                return false;
            }
        }
    }

    private class ValidasiB1R6 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() >= 1 && field.getInt() <= 20) {
                Blok123Model.getInstance().setB1R6(String.format("%02d", field.getInt()));
                return true;
            } else {
                errorMessage("No Urut Sampel Rumah Tangga hanya bernilai 01-20 !");
                return false;
            }
        }
    }

    private class ValidasiNKS implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            String nks = panelBlok123.getTextFieldB1R2().getText() + panelBlok123.getTextFieldB1R5().getText() + panelBlok123.getTextFieldB1R6().getText();
            if (field.getText().equals(nks)) {
                Blok123Model.getInstance().setNKS(nks);
                return true;
            } else {
                errorMessage("No kode sampel tidak sesuai");
                return false;
            }

        }
    }
    
    
    private class ValidasiB1R8 implements Validator{

        @Override
        public boolean onCheckValue(TextField field) {
            String namaKRT = field.getText().trim();
            Blok123Model.getInstance().setB1R8(namaKRT);
            PanelBlok4.getInstance().getListTextFieldBlok4().get(0).setText(namaKRT);
            PanelBlok4.getInstance().getListTextFieldBlok4().get(1).setText("1");
            return true;
        }
        
    }

    private class ValidasiB2R1 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() < 1) {
                errorMessage("Jumlah ART minimal 1");
                return false;
            } else if (field.getInt() <= 15) {
                List<TextField> listTextFieldBlok4 = PanelBlok4.getInstance().getListTextFieldBlok4();
                for (int i = 0; i < field.getInt(); i++) {
                    int awal = i * 11;
                    int akhir = awal + 10;
                    for (int j = awal; j <= akhir; j++) {
                        listTextFieldBlok4.get(j).setEnabled(true);
                    }
                }

                for (int i = field.getInt(); i < 15; i++) {
                    int awal = i * 11;
                    int akhir = awal + 10;
                    for (int j = awal; j <= akhir; j++) {
                        listTextFieldBlok4.get(j).setEnabled(false);
                        listTextFieldBlok4.get(j).setText("");
                    }
                }
                Blok123Model.getInstance().setB2R1(field.getText());
                PanelBlok4.getInstance().getValidasiBlok4().dataRuta = new String[field.getInt()][2];
                return true;
            } else {
                errorMessage("Jumlah ART tidak boleh lebih dari 15");
                return false;
            }
        }
    }

    private class ValidasiB2R2 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() < 1) {
                errorMessage("Jumlah ART yang berumur 10 tahun ke atas minimal satu");
                return false;
            } else if (field.getInt() > panelBlok123.getTextFieldB2R1().getInt()) {
                errorMessage("Jumlah ART yang berumur 10 tahun ke atas tidak boleh lebih dari jumlah ART");
                return false;
            } else {
                Blok123Model.getInstance().setB2R2(field.getText());
                return true;
            }
        }
    }

    private class ValidasiB2R3 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() <= panelBlok123.getTextFieldB2R1().getInt()) {
                Blok123Model.getInstance().setB2R3(String.format("%02d", field.getInt()));
                return true;
            } else {
                errorMessage("Nomor urut pemberi informasi harus lebih kecil dari jumlah ART");
                return false;
            }
        }
    }
    
    private class ValidasiB3R1 implements Validator{

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().setB3R1(field.getText());
                Blok123Model.getInstance().getValidasiB3R1(field.getText());
                return true;
            } catch (EntityException ex) {
                errorMessage("NIM pencacah tidak sesuai");
                return false;
            }
        }
        
    }
    
    private class ValidasiB3R4 implements Validator{

        @Override
        public boolean onCheckValue(TextField field) {
            try {
                Blok123Model.getInstance().setB3R4(field.getText());
                Blok123Model.getInstance().getValidasiB3R4(field.getText());
                return true;
            } catch (EntityException ex) {
                errorMessage("NIM kortim tidak sesuai");
                return false;
            }
        }
        
    }

    private class ValidasiB3R2a implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() > 15 || field.getInt() < 8) {
                errorMessage("Tanggal pencacahan antara 8-15 Maret 2012");
                return false;
            } else {
                Blok123Model.getInstance().setB3R2a(String.format("%02d", field.getInt()));
                panelBlok123.getTextFieldB3R2b().setText("03");
                return true;
            }
        }
    }

    private class ValidasiB3R5a implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() < panelBlok123.getTextFieldB3R2a().getInt()) {
                errorMessage("Tanggal pemeriksaan tidak boleh kurang dari tanggal pencacahan");
                return false;
            }
            if (field.getInt() > 15) {
                errorMessage("Tanggal pemeriksaan tidak boleh lebih dari 15 Maret 2012");
                return false;
            } else {
                Blok123Model.getInstance().setB3R5a(String.format("%02d", field.getInt()));
                panelBlok123.getTextFieldB3R5b().setText("03");
                CardKues.getCard().next();
                return true;
            }
        }
    }

    private class ValidasiB3R2b implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() != 3) {
                errorMessage("Isikan bulan Maret");
                return false;
            } else {
                Blok123Model.getInstance().setB3R2b(String.format("%02d", field.getInt()));
                return true;
            }
        }
    }

    private class ValidasiB3R5b implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            if (field.getInt() != 3) {
                errorMessage("Isikan bulan Maret");
                return false;
            } else {
                Blok123Model.getInstance().setB3R5b(String.format("%02d", field.getInt()));
                CardKues.getCard().next();
                return true;
            }
        }
    }
}
